package com.wss.module.market.ui.main.adapter;

import com.felipecsl.asymmetricgridview.*;
import com.orhanobut.logger.Logger;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.bean.Event;
import com.wss.common.constants.EventAction;
import com.wss.common.utils.EventBusUtils;
import com.wss.common.utils.ImageUtils;
import com.wss.module.market.ResourceTable;
import com.wss.module.market.bean.GoodsInfo;
import com.wss.module.market.utils.ShoppingCartUtils;
import ohos.agp.components.*;
import ohos.agp.render.Canvas;
import ohos.agp.text.RichTextBuilder;
import ohos.agp.text.TextForm;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MarketMainProvider extends AgvRecyclerViewAdapter<MarketMainProvider.ViewHolder> {

    private OnListItemClickListener<GoodsInfo> listener;
    private Context context;
    private List<GoodsInfo> list;

    public MarketMainProvider(Context context, List<GoodsInfo> list, OnListItemClickListener<GoodsInfo> listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }

    public void appendItems(List<GoodsInfo> data) {
        if (list != null) {
            list.addAll(data);
        }
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public GoodsInfo getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public ViewHolder onCreateViewHolder(ComponentContainer componentContainer, int i) {

        return new ViewHolder(context);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.bind(i, list.get(i));
    }

    class ViewHolder extends BaseViewHolder {

        Image ivImg;
        Text tvTitle;
        Text tvPrice;
        Text tvOldPrice;
        Image ivAddCart;

        public ViewHolder(Context context) {
            super(LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_market_item_of_market_list,
                            null,
                            false));

            ivImg = (Image) getComponent().findComponentById(ResourceTable.Id_iv_img);
            tvTitle = (Text) getComponent().findComponentById(ResourceTable.Id_tv_title);
            tvPrice = (Text) getComponent().findComponentById(ResourceTable.Id_tv_price);
            tvOldPrice = (Text) getComponent().findComponentById(ResourceTable.Id_tv_old_price);
            ivAddCart = (Image) getComponent().findComponentById(ResourceTable.Id_iv_add_cart);
        }

        void bind(int layoutPosition, @NotNull GoodsInfo data) {

            final GoodsInfo goodsInfo = data;

            ImageUtils.loadImage(ivImg, data.getGoodsMasterImg());

            tvTitle.setText(data.getGoodsName());
            tvPrice.setText(data.getGoodsPrice());
            tvOldPrice.setText(data.getGoodsOldPrice());
            ivAddCart.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    //加入购物车
                    ShoppingCartUtils.addCartGoods(goodsInfo);
                    EventBusUtils.sendEvent(new Event(EventAction.EVENT_SHOPPING_CART_CHANGED));
                }
            });

            ivImg.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    listener.onItemClick(data, layoutPosition);
                }
            });
        }
    }
}
